import { _decorator, Component } from 'cc';
const { ccclass, property } = _decorator;
import * as fgui from "fairygui-cc";
import CardEntity from '../entity/CardEntity';
import { GameController } from '../manager/GameController';

export default class CardCtrl {
    private _selectionCard: fgui.GComponent = null;

    private _cardInfoArray: Array<cardInfo> = [];

    public _doubleCardList: Array<CardEntity> = []
    public _tripleCardList: Array<CardEntity> = []

    public cardSelectedHandle: (card: cardInfo) => void;

    private _display: fgui.Controller = null;
    private _controller: fgui.Controller = null;

    private _detailTitle: fgui.GComponent = null;
    private _detailView: fgui.GComponent = null;

    private _animationRunning = false;

    constructor(node: fgui.GComponent, controller: fgui.Controller) {
        this._selectionCard = node;
        this._display = controller;

        this._detailTitle = node.getChild('n4').asCom;
        this._detailView = node.getChild('n3').asCom;

        this._controller = this._selectionCard.getController('firstLeg');

        ["n11", "n10"].forEach((name, index) => {
            const node = this._selectionCard.getChild(name).asCom;
            const card = new CardEntity(node)
            card.clickHandle = (info) => {
                this.cardClickEvent(info)
            }
            this._doubleCardList.push(card)
        });

        ["n8", "n7", "n6"].forEach((name, index) => {
            const node = this._selectionCard.getChild(name).asCom;
            const card = new CardEntity(node)
            card.clickHandle = (info) => {
                this.cardClickEvent(info)
            }
            this._tripleCardList.push(card)
        });
    }

    public cardClickEvent(info) {
        if (this._animationRunning === true) return;
        GameController.Instance.sendSelectedCardInfo(info)
        this.seletedCard(info);
    }

    public updateCardInfo(infoArray: Array<cardInfo>) {

        this._cardInfoArray = infoArray;
        let index = 0;//牌数 控制器下标
        let list = null;
        if (this._cardInfoArray.length == 2) {
            index = 1
            list = this._doubleCardList;
        }
        else {
            index = 0;
            list = this._tripleCardList;
        }

        if (list == null) return;
        this._controller.setSelectedIndex(index);
        infoArray.forEach((info, idx) => {
            const card: CardEntity = list[idx];
            card.updateInfo(info);
        })
        this._display.setSelectedIndex(1);
    }

    seletedCard(cardInfo: cardInfo) {

        this._animationRunning = true;
        GameController.Instance.updateEnergy(parseInt(cardInfo.shield))

        let list: Array<CardEntity> = null;
        if (this._cardInfoArray.length == 2) {
            list = this._doubleCardList
        }
        else if (this._cardInfoArray.length == 3) {
            list = this._tripleCardList
        }
        if (list == null) return

        this._detailView.visible = false;
        this._detailTitle.visible = false;

        list.forEach((card: CardEntity, idx: number) => {
            if (cardInfo.id != card.info.id) {
                setTimeout(() => {
                    card.playFadeAnimation()
                }, 50 * idx);
            }
            else {
                card.selectedController.setSelectedIndex(1)
                setTimeout(() => {
                    card.playSelectedAnimation()
                    card.selectedFadeHandle = () => {
                        this._detailView.visible = true;
                        this._detailTitle.visible = true;
                        
                        this._animationRunning = false;
                        this.cardSelectedHandle(card.info);
                        this._display.setSelectedIndex(0)
                        list.forEach(card => {
                            card.resetPosition();
                        })
                    }
                }, 100);
            }
        })
    }

    public reset() {
        this._animationRunning = false;
        this._display.setSelectedIndex(0)
    }
}